<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=6.7.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.7.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=6.7.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=6.7.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.7.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.7.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="Python基础要点一1.一行代码实现1–100之和1sum(range(101))">
<meta name="keywords" content="Python,Python基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Python入门问题">
<meta property="og:url" content="http://yoursite.com/2018/12/04/Python入门问题/index.html">
<meta property="og:site_name" content="LF&#39;s Blogs">
<meta property="og:description" content="Python基础要点一1.一行代码实现1–100之和1sum(range(101))">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/1.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/2.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/3.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/6.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/7.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/7-2.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/11-1.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/11.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/12.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/15.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/19.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/20.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/21.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/22.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/23.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/24.jpg">
<meta property="og:image" content="https://lightfisher.github.io/img/Python_question/25.jpg">
<meta property="og:updated_time" content="2019-01-26T11:49:41.421Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python入门问题">
<meta name="twitter:description" content="Python基础要点一1.一行代码实现1–100之和1sum(range(101))">
<meta name="twitter:image" content="https://lightfisher.github.io/img/Python_question/1.jpg">



  <link rel="alternate" href="/atom.xml" title="LF's Blogs" type="application/atom+xml">




  <link rel="canonical" href="http://yoursite.com/2018/12/04/Python入门问题/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Python入门问题 | LF's Blogs</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">LF's Blogs</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">Hungry For Studying...</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-search">

    
    
    
      
    

    

    <a href="/search/" rel="section"><i class="menu-item-icon fa fa-fw fa-search"></i> <br>搜索</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/04/Python入门问题/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Light Fish">
      <meta itemprop="description" content="Hungry For Studying...">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="LF's Blogs">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Python入门问题

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-12-04 22:35:40" itemprop="dateCreated datePublished" datetime="2018-12-04T22:35:40+08:00">2018-12-04</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-01-26 19:49:41" itemprop="dateModified" datetime="2019-01-26T19:49:41+08:00">2019-01-26</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          <span class="post-meta-divider">|</span>
<span title="post.wordcount"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i></span>字数： 2.1k</span>

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Python基础要点一"><a href="#Python基础要点一" class="headerlink" title="Python基础要点一"></a>Python基础要点一</h1><h3 id="1-一行代码实现1–100之和"><a href="#1-一行代码实现1–100之和" class="headerlink" title="1.一行代码实现1–100之和"></a>1.一行代码实现1–100之和</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sum(range(101))</span><br></pre></td></tr></table></figure>
<p><img src="https://lightfisher.github.io/img/Python_question/1.jpg" alt="img"></p>
<a id="more"></a>
<h3 id="2-如何在一个函数内部修改全局变量"><a href="#2-如何在一个函数内部修改全局变量" class="headerlink" title="2.如何在一个函数内部修改全局变量"></a>2.如何在一个函数内部修改全局变量</h3><p>利用<code>global</code>修改全局变量<br></p>
<p><img src="https://lightfisher.github.io/img/Python_question/2.jpg" alt="global"></p>
<h3 id="3-列出5个Python的标准库"><a href="#3-列出5个Python的标准库" class="headerlink" title="3.列出5个Python的标准库"></a>3.列出5个Python的标准库</h3><ul>
<li><code>os</code>: 提供了不少于操作系统相关联的函数</li>
<li><code>sys</code>: 通常用于命令行参数</li>
<li><code>re</code>: 正则匹配</li>
<li><code>math</code>: 数学运算</li>
<li><code>datetime</code>: 处理日期时间</li>
</ul>
<h3 id="4-字典如何删除键和合并两个字典"><a href="#4-字典如何删除键和合并两个字典" class="headerlink" title="4.字典如何删除键和合并两个字典"></a>4.字典如何删除键和合并两个字典</h3><pre><code>删除用 del,合并用 update
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">dic = &#123;&apos;name&apos;:&apos;qg&apos;,&apos;age&apos;:18&#125;</span><br><span class="line">del dic[&apos;name&apos;]</span><br><span class="line">dic2 = &#123;&apos;name&apos;:&apos;fh&apos;&#125;</span><br><span class="line">dic.update(dic2)</span><br></pre></td></tr></table></figure>
<p><br><br><img src="https://lightfisher.github.io/img/Python_question/3.jpg" alt="del update"><br><br></p>
<h3 id="5-谈下python的GIL"><a href="#5-谈下python的GIL" class="headerlink" title="5.谈下python的GIL"></a>5.谈下python的GIL</h3><ul>
<li>GIL是Python的全局解释器锁，同一个进程加入由多个线程运行，一个线程在运行python程序的时候会霸占python解释器(加了一个锁即GIL)<br>，使进程内的其他线程无法运行，等该线程运行完后其他线程才能运行。如果线程运行过程中遇到耗时操作，则解释器锁解开，使其他线程<br>运行。所以在多线程中，线程的运行仍是有先后顺序，并不是同时运行。</li>
<li>多进程中因为每个进程都能被喜用分批资源，相当于每个进程有了个python解释器，多以多进程可以实现多个进程的同时运行，缺点是金城系统资源开销大</li>
</ul>
<h3 id="6-python实现列表去重的方法"><a href="#6-python实现列表去重的方法" class="headerlink" title="6.python实现列表去重的方法"></a>6.python实现列表去重的方法</h3><blockquote>
<p>熟悉使用<code>set</code></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">list=[np.random.randint(10,15) for _ in range(10)]</span><br><span class="line">print(list)</span><br><span class="line">a = set(list)</span><br><span class="line">print(a)</span><br><span class="line">[i for i in a]</span><br></pre></td></tr></table></figure>
<p><br><br><img src="https://lightfisher.github.io/img/Python_question/6.jpg" alt="set"><br><br></p>
<h3 id="7-fun-args-kwargs-中args，-kwargs是什么意思？"><a href="#7-fun-args-kwargs-中args，-kwargs是什么意思？" class="headerlink" title="7.fun(args,**kwargs)中args，**kwargs是什么意思？"></a>7.fun(<em>args,**kwargs)中</em>args，**kwargs是什么意思？</h3><ul>
<li><em>args和</em>kwargs主要用于函数的定义。你可以将不定数量的参数传递给一个函数。这里的不定的意思是：预先并不知道，函数使用者会传递多少个参数给你，所以在这个场景下使用这两个关键字。*args是用来发送一个非键值对的可变数量的参数列表给一个函数。这里有一个例子</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">def demo(args_f,*args_v):</span><br><span class="line">    print(args_f)</span><br><span class="line">    for x in args_v:</span><br><span class="line">        print(x)</span><br><span class="line">demo(&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;,e&apos;)</span><br></pre></td></tr></table></figure>
<p><br><br><img src="https://lightfisher.github.io/img/Python_question/7.jpg" alt="*args"><br><br></p>
<ul>
<li><strong>kwargs 允许你将不定长度的键值对传递给一个函数。如果你想要一个函数里处理带名字的参数，你因该使用</strong>kwargs，这里有一个例子</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def demo(**args_v):</span><br><span class="line">    for k,v in args_v.items():</span><br><span class="line">        print(k,v)</span><br><span class="line">demo(name=&apos;light&apos;,name=&apos;fish)</span><br></pre></td></tr></table></figure>
<p><br><br><img src="https://lightfisher.github.io/img/Python_question/7-2.jpg" alt="*kwargs"><br><br></p>
<h3 id="8-python2和python3的range的区别"><a href="#8-python2和python3的range的区别" class="headerlink" title="8.python2和python3的range的区别"></a>8.python2和python3的range的区别</h3><blockquote>
<p>python2返回一个列表，python3返回一个迭代器，节约内存</p>
</blockquote>
<h3 id="9-一句话解释什么样的语言能过用装饰器"><a href="#9-一句话解释什么样的语言能过用装饰器" class="headerlink" title="9.一句话解释什么样的语言能过用装饰器"></a>9.一句话解释什么样的语言能过用装饰器</h3><blockquote>
<p>函数可以作为参数传递的语言，可以使用装饰器</p>
</blockquote>
<h3 id="10-python内建函数类型有哪些"><a href="#10-python内建函数类型有哪些" class="headerlink" title="10.python内建函数类型有哪些"></a>10.python内建函数类型有哪些</h3><ul>
<li>整型 – <code>int</code></li>
<li>布尔型 – <code>bool</code></li>
<li>字符串 – <code>str</code></li>
<li>列表 – <code>list</code></li>
<li>元组 – <code>tuple</code></li>
<li>字典 – <code>dict</code></li>
<li>集合 – <code>set</code></li>
</ul>
<h3 id="11-简述面向对象中new和init的区别"><a href="#11-简述面向对象中new和init的区别" class="headerlink" title="11.简述面向对象中new和init的区别"></a>11.简述面向对象中<strong>new</strong>和<strong>init</strong>的区别</h3><ul>
<li><p><strong>init</strong>是初始化方法，创建对象后，就立刻被默认调用，相当于C语言的构造函数，可接受参数，如下图：<br><br><br><img src="https://lightfisher.github.io/img/Python_question/11-1.jpg" alt="__init__"><br><br></p>
</li>
<li><p><strong>new</strong>至少要有一个参数cls，代表是当前类，此参数在实例化的时候由Python解释器自动识别</p>
</li>
<li><strong>new</strong>必须要有返回值，返回实例化出来的实例，这点在自己实现<strong>new</strong>时要特别注意，可以return父类(通过super(当前类，cls)) <strong>new</strong>出来的实例，或者直接是object的<strong>new</strong>出来的实例</li>
<li><strong>init</strong>有一个参数self,就是这个<strong>new</strong>返回的实例，<strong>init</strong>在<strong>new</strong>的基础上可以完成一些其他初始化的动作，<strong>init</strong>不需要返回值</li>
<li>如果<strong>new</strong>创建的是当前类的实例，会自动调用<strong>init</strong>函数，通过return语句里面调用<strong>new</strong>函数的第一个参数cls来保证是当前类实例，如果是其他类的类名；那么实际创建返回的就是其他类的实例，其实就不会调用当前类的<strong>init</strong>函数，也不会调用其他类的<strong>init</strong>函数<br><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Test(object):</span><br><span class="line">    def __init__(self):</span><br><span class="line">        print(&apos;这是 init 方法&apos;,self)</span><br><span class="line">    def __new__(cls):</span><br><span class="line">        print(&apos;这是cls的ID&apos;,id(cls))</span><br><span class="line">        print(&apos;这是 new 方法&apos;,object.__new__(cls))</span><br><span class="line">        return object.__new__(cls)</span><br><span class="line">Test()</span><br><span class="line">print(&apos;这是A的ID&apos;,id(Test))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://lightfisher.github.io/img/Python_question/11.jpg" alt="__new__"></p>
<h3 id="12-列表-1-2-3-4-5-请使用map函数输出-1-4-9-16-25-并使用列表推导式提取出大于10的数"><a href="#12-列表-1-2-3-4-5-请使用map函数输出-1-4-9-16-25-并使用列表推导式提取出大于10的数" class="headerlink" title="12.列表[1,2,3,4,5],请使用map函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数"></a>12.列表[1,2,3,4,5],请使用map函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数</h3><ul>
<li><code>map()</code>函数的第一个参数是fun</li>
<li><code>map()</code>生成的生成器，只能使用一次<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list1 = list(range(1,6))</span><br><span class="line">res = map(lambda x: x**2,list1)</span><br><span class="line">res_list = [i for i in res if i &gt; 10]</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://lightfisher.github.io/img/Python_question/12.jpg" alt="map"></p>
<h3 id="13-python中生成随机数的若干方法"><a href="#13-python中生成随机数的若干方法" class="headerlink" title="13.python中生成随机数的若干方法"></a>13.python中生成随机数的若干方法</h3><ul>
<li>random.uniform(a,b) 闭区间取浮点数,a,b可换序</li>
<li>random.randint(q,b) 闭区间取整型数，a,b不可换序</li>
<li>random.randrange(a,b,c) b开,取a向上递增2数的随机数</li>
<li>random.choice(‘12345asd?”‘) 随机取数</li>
<li>random.sample(‘abcdefg’,3) 随机取三个数</li>
</ul>
<h3 id="14-避免转义给字符串加什么字符表示原是字符串？"><a href="#14-避免转义给字符串加什么字符表示原是字符串？" class="headerlink" title="14.避免转义给字符串加什么字符表示原是字符串？"></a>14.避免转义给字符串加什么字符表示原是字符串？</h3><ul>
<li><code>r</code>,表示需要原始字符串</li>
</ul>
<h3 id="15-python中断言的方法"><a href="#15-python中断言的方法" class="headerlink" title="15.python中断言的方法"></a>15.python中断言的方法</h3><ul>
<li>assert()方法，断言成功程序继续运行，断言失败，则程序报错<br><img src="https://lightfisher.github.io/img/Python_question/15.jpg" alt="assert"></li>
</ul>
<h3 id="16-数据库中student有重复的name，消除重复，请写sql语句"><a href="#16-数据库中student有重复的name，消除重复，请写sql语句" class="headerlink" title="16.数据库中student有重复的name，消除重复，请写sql语句"></a>16.数据库中student有重复的name，消除重复，请写sql语句</h3><pre><code>select distinct name from studnet
</code></pre><h3 id="17-10个Linux常用命令"><a href="#17-10个Linux常用命令" class="headerlink" title="17.10个Linux常用命令"></a>17.10个Linux常用命令</h3><pre><code>ls pwd cd touch rm mkdir tree cp mv cat vi more echo grep
</code></pre><h3 id="18-python2和python3的区别"><a href="#18-python2和python3的区别" class="headerlink" title="18.python2和python3的区别?"></a>18.python2和python3的区别?</h3><ul>
<li>Python3使用print必须要用小括号包裹打印内容，Python2可以使用小括号也可以空格</li>
<li>Python2 range(10) 返回的实列表，Python3返回的是迭代器，节约内存</li>
<li>Python2中使用ascii编码，Python3中使用utf-8编码</li>
<li>Python3中str表示字符串序列，byte表示字节序列</li>
<li>Python2中unicode表示字符串序列，str表示字节序列</li>
<li>Python2为了正常显示中文，要coding申明，Python3不需要</li>
<li>Python2是raw_input(),Python 是input()</li>
</ul>
<h3 id="19-列出Python中可变数据和不可变数据类型，并简述原理"><a href="#19-列出Python中可变数据和不可变数据类型，并简述原理" class="headerlink" title="19.列出Python中可变数据和不可变数据类型，并简述原理"></a>19.列出Python中可变数据和不可变数据类型，并简述原理</h3><ul>
<li>不可变数据类型：数值型、字符串型string和元组tuple<br>不允许变量的值发生变化，如果改变了变量的值，相当于创建了一个对象，而对于相同的值的对象，在内存中则只有一个对象(一个地址)<br><br><img src="https://lightfisher.github.io/img/Python_question/19.jpg" alt="assert"></li>
<li>可变数据类型：列表list和字典dict；<br>允许变量的值发生变化，而不会新建一个对象，变量引用的对象的地址也不会发生变化，不过对于相同的值的不同对象，在内存中则会存在不同的对象，即每个对象都有自己的地址，相当于内存中对于同值的对象保存了多份，这里不存在引用计数，是实实在在的对象<br><br><img src="https://lightfisher.github.io/img/Python_question/20.jpg" alt="assert"></li>
</ul>
<h3 id="20-去重并从小到大排序输出"><a href="#20-去重并从小到大排序输出" class="headerlink" title="20.去重并从小到大排序输出"></a>20.去重并从小到大排序输出</h3><ul>
<li>相信你肯定学过<code>set</code>集合，<code>set</code>是可以实现去重的，不失为一种好的方法，然后利用sort方法进行排序，默认参数reverse = False就是从小到大排序，为True，是从大到小</li>
<li><p>list是可变数据类型，所以list.sort()没有返回值，所以注释掉的代码写法不正确</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">s = &apos;amnshgisabcd&apos;</span><br><span class="line">s = set(s)</span><br><span class="line">s = list(s)</span><br><span class="line">s.sort()</span><br><span class="line"># s = s.sort()</span><br><span class="line">res = &apos;&apos;.join(s)</span><br></pre></td></tr></table></figure>
</li>
<li><p>利用集合或者生成器解决问题，去重并保持顺序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">def dupe(items):</span><br><span class="line">	seen = set() #创建一个空的集合</span><br><span class="line">      for item in items:</span><br><span class="line">         if item not in seen:</span><br><span class="line">     	   yield item</span><br><span class="line">            seen.add(item)</span><br><span class="line">a = [1,3,6,1,8,1,5]</span><br><span class="line">list(dupe(a))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://lightfisher.github.io/img/Python_question/21.jpg" alt="yield"></p>
<ul>
<li>消除元素不可哈希：如字典类型</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">def rem(items, key=None):</span><br><span class="line">    seen = set()  # 创建一个空的集合</span><br><span class="line">    for item in items:</span><br><span class="line">        va = item if key is None else key(item)</span><br><span class="line">        if va not in seen:</span><br><span class="line">            yield item</span><br><span class="line">            seen.add(va)</span><br><span class="line">a = [&#123;&apos;x&apos;:1, &apos;y&apos;:2&#125;, &#123;&apos;x&apos;:1,&apos;y&apos;: 3&#125;, &#123;&apos;x&apos;:1, &apos;y&apos;:2&#125;, &#123;&apos;x&apos;:2, &apos;y&apos;:4&#125;]</span><br><span class="line">list(rem(a, key=lambda d: (d[&apos;x&apos;],d[&apos;y&apos;])))</span><br></pre></td></tr></table></figure>
<p><img src="https://lightfisher.github.io/img/Python_question/22.jpg" alt="set"></p>
<h3 id="21-lambda的基本使用，字典根据键值从小到大排序，用sorted函数"><a href="#21-lambda的基本使用，字典根据键值从小到大排序，用sorted函数" class="headerlink" title="21.lambda的基本使用，字典根据键值从小到大排序，用sorted函数"></a>21.lambda的基本使用，字典根据键值从小到大排序，用sorted函数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">dict1 = &#123;&apos;name&apos;: &apos;zs&apos;, &apos;age&apos;: 18, &apos;city&apos;: &apos;杭州&apos;, &apos;tel&apos;: &apos;1234&apos;&#125;</span><br><span class="line">list1 = sorted(</span><br><span class="line">    dict1.keys(),</span><br><span class="line">    key=lambda d: d[0],</span><br><span class="line">    reverse=False)  # reverse 为True是降序</span><br><span class="line">print(list1)</span><br><span class="line">print(dict1.items())</span><br><span class="line">print(dict1.keys())</span><br><span class="line">print(dict1.values())</span><br></pre></td></tr></table></figure>
<p><img src="https://lightfisher.github.io/img/Python_question/23.jpg" alt="img"></p>
<h3 id="22-利用collections库的Counter方法统计字符串每个单词的出现次数"><a href="#22-利用collections库的Counter方法统计字符串每个单词的出现次数" class="headerlink" title="22.利用collections库的Counter方法统计字符串每个单词的出现次数"></a>22.利用collections库的Counter方法统计字符串每个单词的出现次数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">from collections import Counter</span><br><span class="line">a = &apos;amsjdasduqwenasdlaqiasmd&apos;</span><br><span class="line">res = Counter(a)</span><br><span class="line">print(res)</span><br></pre></td></tr></table></figure>
<p><img src="https://lightfisher.github.io/img/Python_question/24.jpg" alt="collection"></p>
<h3 id="23-filter函数的简单用法"><a href="#23-filter函数的简单用法" class="headerlink" title="23.filter函数的简单用法"></a>23.filter函数的简单用法</h3><ul>
<li>a = list(range(1,10)) 求出所有奇数，并构造新列表<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = list(range(1,10))</span><br><span class="line">newlist = [i for i in filter(lambda x: x % 2, a)]</span><br><span class="line">print(newlist)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://lightfisher.github.io/img/Python_question/25.jpg" alt="filter"></p>
<h3 id="24-列表推导式求上述问题"><a href="#24-列表推导式求上述问题" class="headerlink" title="24.列表推导式求上述问题"></a>24.列表推导式求上述问题</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = list(range(1, 10))</span><br><span class="line">newlist = [i for i in a if i % 2 == 1]</span><br><span class="line">print(newlist)</span><br></pre></td></tr></table></figure>
<h3 id="25-1-、-1-、-“1”-分别是什么类型"><a href="#25-1-、-1-、-“1”-分别是什么类型" class="headerlink" title="25. (1,)、(1)、(“1”)分别是什么类型"></a>25. (1,)、(1)、(“1”)分别是什么类型</h3><ul>
<li>(1) 是int类型</li>
<li>(“1”) 是str类型</li>
<li>(1,) 是元组类型</li>
</ul>

      
    </div>



    

    
    
    

    <div>
      
      <div>

    <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>

</div>
      
    </div>

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
            <a href="/tags/Python基础/" rel="tag"># Python基础</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/05/Hexo-github搭建个人博客一/" rel="prev" title="Hexo+github搭建个人博客一">
                Hexo+github搭建个人博客一 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Light Fish">
            
              <p class="site-author-name" itemprop="name">Light Fish</p>
              <p class="site-description motion-element" itemprop="description">Hungry For Studying...</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">35</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">22</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/QGtiger" title="GitHub &rarr; https://github.com/QGtiger" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://weibo.com/yourname" title="Weibo &rarr; https://weibo.com/yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://twitter.com/yourname" title="Twitter &rarr; https://twitter.com/yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Python基础要点一"><span class="nav-number">1.</span> <span class="nav-text">Python基础要点一</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-一行代码实现1–100之和"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.一行代码实现1–100之和</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-如何在一个函数内部修改全局变量"><span class="nav-number">1.0.2.</span> <span class="nav-text">2.如何在一个函数内部修改全局变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-列出5个Python的标准库"><span class="nav-number">1.0.3.</span> <span class="nav-text">3.列出5个Python的标准库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-字典如何删除键和合并两个字典"><span class="nav-number">1.0.4.</span> <span class="nav-text">4.字典如何删除键和合并两个字典</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-谈下python的GIL"><span class="nav-number">1.0.5.</span> <span class="nav-text">5.谈下python的GIL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-python实现列表去重的方法"><span class="nav-number">1.0.6.</span> <span class="nav-text">6.python实现列表去重的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-fun-args-kwargs-中args，-kwargs是什么意思？"><span class="nav-number">1.0.7.</span> <span class="nav-text">7.fun(args,**kwargs)中args，**kwargs是什么意思？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-python2和python3的range的区别"><span class="nav-number">1.0.8.</span> <span class="nav-text">8.python2和python3的range的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-一句话解释什么样的语言能过用装饰器"><span class="nav-number">1.0.9.</span> <span class="nav-text">9.一句话解释什么样的语言能过用装饰器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-python内建函数类型有哪些"><span class="nav-number">1.0.10.</span> <span class="nav-text">10.python内建函数类型有哪些</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-简述面向对象中new和init的区别"><span class="nav-number">1.0.11.</span> <span class="nav-text">11.简述面向对象中new和init的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-列表-1-2-3-4-5-请使用map函数输出-1-4-9-16-25-并使用列表推导式提取出大于10的数"><span class="nav-number">1.0.12.</span> <span class="nav-text">12.列表[1,2,3,4,5],请使用map函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13-python中生成随机数的若干方法"><span class="nav-number">1.0.13.</span> <span class="nav-text">13.python中生成随机数的若干方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#14-避免转义给字符串加什么字符表示原是字符串？"><span class="nav-number">1.0.14.</span> <span class="nav-text">14.避免转义给字符串加什么字符表示原是字符串？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#15-python中断言的方法"><span class="nav-number">1.0.15.</span> <span class="nav-text">15.python中断言的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#16-数据库中student有重复的name，消除重复，请写sql语句"><span class="nav-number">1.0.16.</span> <span class="nav-text">16.数据库中student有重复的name，消除重复，请写sql语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#17-10个Linux常用命令"><span class="nav-number">1.0.17.</span> <span class="nav-text">17.10个Linux常用命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#18-python2和python3的区别"><span class="nav-number">1.0.18.</span> <span class="nav-text">18.python2和python3的区别?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#19-列出Python中可变数据和不可变数据类型，并简述原理"><span class="nav-number">1.0.19.</span> <span class="nav-text">19.列出Python中可变数据和不可变数据类型，并简述原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#20-去重并从小到大排序输出"><span class="nav-number">1.0.20.</span> <span class="nav-text">20.去重并从小到大排序输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#21-lambda的基本使用，字典根据键值从小到大排序，用sorted函数"><span class="nav-number">1.0.21.</span> <span class="nav-text">21.lambda的基本使用，字典根据键值从小到大排序，用sorted函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#22-利用collections库的Counter方法统计字符串每个单词的出现次数"><span class="nav-number">1.0.22.</span> <span class="nav-text">22.利用collections库的Counter方法统计字符串每个单词的出现次数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#23-filter函数的简单用法"><span class="nav-number">1.0.23.</span> <span class="nav-text">23.filter函数的简单用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#24-列表推导式求上述问题"><span class="nav-number">1.0.24.</span> <span class="nav-text">24.列表推导式求上述问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#25-1-、-1-、-“1”-分别是什么类型"><span class="nav-number">1.0.25.</span> <span class="nav-text">25. (1,)、(1)、(“1”)分别是什么类型</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Light Fish</span>

  

  
</div>


<!-- 新增访客统计代码 -->

<div class="busuanzi-count">
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="site-uv">
      <i class="fa fa-user"></i>
      访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv"></span> 人
    </span>
    <div class="powered-by"></div>
    <span class="site-uv">
      <i class="fa fa-eye"></i>
      访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv"></span> 次
    </span>
    <!-- 博客字数统计 -->
    <span class="site-pv">
      <i class="fa fa-pencil"></i>
      博客全站共： <span class="post-count">31.7k</span> 字
    </span>
</div>
<!-- 新增访客统计代码 END-->

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>  
<script>
    var now = new Date(); 
    function createtime() { 
        var grt= new Date("12/03/2018 12:49:00");//此处修改你的建站时间或者网站上线时间 
        now.setTime(now.getTime()+250); 
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days); 
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours); 
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum); 
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;} 
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum); 
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;} 
        document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 "; 
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒"; 
    } 
setInterval("createtime()",250);
</script>
        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=6.7.0"></script>

  <script src="/js/src/motion.js?v=6.7.0"></script>



  
  


  <script src="/js/src/schemes/muse.js?v=6.7.0"></script>



  
  <script src="/js/src/scrollspy.js?v=6.7.0"></script>
<script src="/js/src/post-details.js?v=6.7.0"></script>



  


  <script src="/js/src/bootstrap.js?v=6.7.0"></script>



  


  


  

  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
